package com.bluesky.jwf.component.list;

import java.util.List;

import com.bluesky.javawebbrowser.domain.html.tags.Tag;
import com.bluesky.jwf.ComponentFactory;
import com.bluesky.jwf.component.Button;
import com.bluesky.jwf.component.Component;
import com.bluesky.jwf.component.HyperLinkButton;
import com.bluesky.jwf.component.TextField;
import com.bluesky.jwf.component.event.AbstractEventHandler;
import com.bluesky.jwf.component.event.EventHandler;

public class PaginationListView extends ListView {

	protected int pageSize = 10;
	/*
	 * start from 1
	 */
	protected int pageNumber = 1;

	protected HyperLinkButton previousButton;
	protected HyperLinkButton nextButton;

	protected TextField pageNumberToGoField;
	protected Button goButton;

	protected Tag currentTag;
	protected Tag totalTag;

	private CollectionProvider listProvider;

	@Override
	public void init(String html) {

		super.init(html);

		previousButton = (HyperLinkButton) ComponentFactory.getInstance()
				.createComponent("HyperLinkButton");
		previousButton.setText("Previous");
		previousButton.setClickHandler(new AbstractEventHandler() {
			@Override
			public void handle(Component target, Object parameter) {
				setPageNumber(pageNumber - 1);

				dataBind();
			}
		});
		loadByJwfClass("previous").replace(previousButton);

		nextButton = (HyperLinkButton) ComponentFactory.getInstance()
				.createComponent("HyperLinkButton");
		nextButton.setText("Next");
		nextButton.setClickHandler(new AbstractEventHandler() {
			@Override
			public void handle(Component target, Object parameter) {

				setPageNumber(pageNumber + 1);

				dataBind();
			}
		});
		loadByJwfClass("next").replace(nextButton);

		pageNumberToGoField = (TextField) ComponentFactory.getInstance()
				.createComponent("TextField");
		pageNumberToGoField.setSize(2);
		pageNumberToGoField.setText("1");
		loadByJwfClass("pageNumberToGo").replace(pageNumberToGoField);

		goButton = (Button) ComponentFactory.getInstance().createComponent(
				"Button");
		goButton.setText("Go");
		goButton.setClickHandler(new AbstractEventHandler() {
			@Override
			public void handle(Component target, Object parameter) {
				try {
					int pn = Integer.valueOf(pageNumberToGoField.getText());
					setPageNumber(pn);

					dataBind();

				} catch (Exception e) {
				}

			}
		});
		loadByJwfClass("go").replace(goButton);

		currentTag = loadByJwfClass("current");
		totalTag = loadByJwfClass("total");
	}

	protected void refreshButtons() {

		previousButton.setDisable(pageNumber <= 1);

		nextButton.setDisable(pageNumber >= getTotalPageNumber());
	}

	public CollectionProvider getListProvider() {
		return listProvider;
	}

	public void setListProvider(CollectionProvider listProvider) {
		this.listProvider = listProvider;

		String s = String.valueOf(getTotalPageNumber());
		totalTag.setBodyAndParse(s);
	}

	@Override
	public void setModel(Object data) {
		super.setModel(data);

		setListProvider(new ListCollectionProvider((List) data));

	}

	@Override
	public void dataBind() {
		listTag.removeAllChildren();
		int startIndex = (pageNumber - 1) * pageSize;
		int endIndex = pageNumber * pageSize - 1;

		if (startIndex < 0)
			startIndex = 0;

		if (endIndex >= listProvider.getCount())
			endIndex = listProvider.getCount() - 1;

		List pageList = listProvider.get(startIndex, endIndex);
		int i = startIndex;
		for (Object o : pageList) {

			ListItem listItem = (ListItem) ComponentFactory.getInstance()
					.createComponent(itemClass.getName(), itemTag.toHtml());

			listItem.setListView(this);
			listItem.setIndex(i);
			listItem.setModel(o);

			listTag.addChild(listItem);

			i++;
		}

		refreshButtons();
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageNumber() {
		return pageNumber;
	}

	public void setPageNumber(int pageNumber) {
		if (pageNumber < 1 || pageNumber > getTotalPageNumber())
			return;

		this.pageNumber = pageNumber;

		String s = String.valueOf(pageNumber);
		currentTag.setBodyAndParse(s);
	}

	public int getTotalPageNumber() {
		int t = listProvider.getCount() / pageSize;
		if (listProvider.getCount() % pageSize > 0)
			t++;

		return t;
	}

}
